package com.njxzc.igo.controller.shoppingcart;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.njxzc.igo.model.User;
import com.njxzc.igo.service.ShoppingCartService;
import com.njxzc.igo.service.ShoppingCartServiceImpl;
import com.njxzc.igo.utils.AiGouUtil;

@WebServlet(name="deleteBatch",urlPatterns="/deleteBatch.do")
public class DeleteBatchShoppingCartController extends HttpServlet {
	private ShoppingCartService scs = new ShoppingCartServiceImpl();
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			HttpSession session  = request.getSession();
			String []ids = request.getParameterValues("shopids[]");
			
			List<String>shoppingCartIdsList = AiGouUtil.stringStrToList(ids);
			
			scs.deleteChoosenItems(shoppingCartIdsList);
			
			User user = (User) session.getAttribute("u");
			List shoppingCart = scs.loadmyCart(user.getUsername());
			Map shoppingMap = AiGouUtil.groupBy(shoppingCart);
			
			if(shoppingCart.size()==0){
				session.setAttribute("map3", null);
			}else{
				session.setAttribute("map3", shoppingMap);	
			}
	}
}
